package com.hengda.museumonline.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.LoginBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.utils.SharedPreferenceUtil;
import com.litesuits.common.assist.Base64;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/9/22 17:05
 * @E-mail：jeffy12138@126.com
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private android.widget.TextView tvregister;
    private android.widget.EditText username;
    private android.widget.EditText password;
    private android.widget.LinearLayout login;
    private LoginBean loginBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        this.login = (LinearLayout) findViewById(R.id.login);
        login.setOnClickListener(this);
        this.password = (EditText) findViewById(R.id.password);
        this.username = (EditText) findViewById(R.id.username);
        this.tvregister = (TextView) findViewById(R.id.tv_register);
        tvregister.setOnClickListener(this);
        System.out.println("加密密码：" + Base64.encodeToString("hengda".getBytes(), Base64.DEFAULT));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_register:
                Intent intent = new Intent(LoginActivity.this, PhoneRegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login:
                login();
                break;
        }
    }

    private void login() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "login/index", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(LoginActivity.this).versionName);
        request.add("username", "admin@qq.com");
        request.add("password", Base64.encodeToString("hengda".getBytes(), Base64.DEFAULT));

        CallServer.getRequestInstance().add(LoginActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
                dialog.show();
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    loginBean = gson.fromJson(response.get().toString(), LoginBean.class);
                    if (1 == loginBean.getStatus()) {
                        if ("1".equals(loginBean.getData().getUser_status())) {
                            spUtil.setToken(loginBean.getData().getToken());
                            Intent intent = new Intent(LoginActivity.this, SettingActivity.class);
                            intent.putExtra("loginBean", loginBean);
                            startActivity(intent);
                            finish();
                        } else if ("2".equals(loginBean.getData().getUser_status())) {
                            getToast("账号没有激活，请到邮箱激活账号后重新登陆");
                            getDoactive(loginBean.getData().getToken());
                        }


                    } else if (0 == loginBean.getStatus()) {
                        getToast(loginBean.getMsg());
                    }

                }

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
            }

            @Override
            public void onFinish(int what) {
                dialog.dismiss();
            }
        });
    }

    private void getDoactive(String token) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "login/doactive", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(LoginActivity.this).versionName);
        request.add("token", token);

        CallServer.getRequestInstance().add(LoginActivity.this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
            }

            @Override
            public void onFinish(int what) {
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CallServer.getRequestInstance().cancelAll();
    }
}
